import { getInfo, deleteUser } from '../../services/profile'
import Toast from '@vant/weapp/toast/toast'
import { checkIfLogin } from '../../utils/check'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    showDialogConfirm:false,
    dialogMsg:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    checkIfLogin()
    this.handleGetInfo()
  },

  async handleGetInfo(){
    let resInfo = await getInfo()
    if(resInfo.code === 200){
      let userInfo = resInfo.user
      this.setData({
        userInfo
      })
    }
  },

  handleDeleteUser(){
    this.setData({
      showDialogConfirm: true,
      dialogMsg: "是否确认注销账号?",
    })
  },

  async confirmInfo(){
    let res = await deleteUser()
    if(res.code === 200){
      Toast.success('注销成功!')
      // wx.removeStorageSync('token')
      // wx.removeStorageSync('userInfo')
      // wx.removeStorageSync('appid')
      // wx.removeStorageSync('secret')
      // if(wx.getStorageSync('spaceId')){
      //   wx.removeStorageSync('spaceId')
      // }
      wx.clearStorage()
      app.globalData.userInfo = null
      wx.reLaunch({
        url: '/pages/login/login',
      })
    }else {
      Toast.fail(`注销失败:${res.msg}`)
    }
  }
})